import request from '../utils/request';
import { DoctorService } from './service/doctorService';
import { invoke } from '@tauri-apps/api/core';

export const fetchData = () => {
    return request({
        url: './mock/table.json',
        method: 'get'
    });
};

export const fetchUserData = () => {
    return request({
        url: './mock/user.json',
        method: 'get'
    });
};

export const fetchRoleData = () => {
    return request({
        url: './mock/role.json',
        method: 'get'
    });
};

interface UploadFileParams extends Record<string, unknown> {
    fileData: number[];
    fileName: string;
}

interface GetFileStreamParams extends Record<string, unknown> {
    fileName: string;
}

export async function uploadFile(file: File): Promise<string> {
    const arrayBuffer = await file.arrayBuffer();
    const fileData = Array.from(new Uint8Array(arrayBuffer));
    const fileName = file.name;
    
    return await invoke<string>('upload_file', {
        fileData,
        fileName
    } as UploadFileParams);
}

export async function getFileStream(fileName: string): Promise<Uint8Array> {
    const data = await invoke<number[]>('get_file_stream', {
        fileName
    } as GetFileStreamParams);
    data.length
    return new Uint8Array(data);
}

export async function listUploadFiles(): Promise<string[]> {
    return await invoke<string[]>('list_upload_files');
}


export { DoctorService };
export { DownloadService } from './service/downloadService';
